package com.moon.es;

import com.moon.dao.TAuctionMapper;
import com.moon.vo.EsTAuction;
import com.moon.vo.TAuctionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("esTAuction")
public class ESTAuctionDataWriter implements ESDataWriter {

    @Autowired
    ESTAuctionRepository estAuctionRepository;

    @Autowired
    TAuctionMapper tAuctionMapper;

    public void dataWrite() {
        //将mysql中的数据全部读取出来,然后写入到es中
        List<TAuctionVo> tAuctionVos = tAuctionMapper.selectAllTAuctionVo();

        List<EsTAuction> esTAuctionList = tAuctionVos.stream().map(tAuctionVo -> {
            EsTAuction esTAuction = new EsTAuction();

            esTAuction.setId(tAuctionVo.getId());
            esTAuction.setProductId(tAuctionVo.getProductId());
            esTAuction.setBasePrice(tAuctionVo.getBasePrice());
            esTAuction.setStepPrice(tAuctionVo.getStepPrice());
            esTAuction.setTopPrice(tAuctionVo.getTopPrice());
            esTAuction.setSecurityPrice(tAuctionVo.getSecurityPrice());
            esTAuction.setBuyerId(tAuctionVo.getBuyerId());
            esTAuction.setNickname(tAuctionVo.getNickname());
            esTAuction.setCreateTime(tAuctionVo.getCreateTime());
            esTAuction.setUpdateTime(tAuctionVo.getUpdateTime());
            esTAuction.setStartTime(tAuctionVo.getStartTime());
            esTAuction.setAuctionTime(tAuctionVo.getAuctionTime());
            esTAuction.setStatus(tAuctionVo.getStatus());
            esTAuction.setJoinCount(tAuctionVo.getJoinCount());
            esTAuction.setPid(tAuctionVo.getPid());
            esTAuction.setSellerId(tAuctionVo.getSellerId());
            esTAuction.setProductName(tAuctionVo.getProductName());
            esTAuction.setProductDetail(tAuctionVo.getProductDetail());
            esTAuction.setPstatus(tAuctionVo.getPstatus());
            esTAuction.setProductImg(tAuctionVo.getProductImg());
            return esTAuction;
        }).collect(Collectors.toList());
        estAuctionRepository.saveAll(esTAuctionList);

    }

}
